package com.example.demo.service.impl;

import com.example.demo.entity.Order;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单服务实现类
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void createOrder(Order order) {
        orderMapper.insert(order);
    }

    @Override
    public Order updateOrderStatus(Integer orderId, String status) {
        Order order = getOrder(orderId);
        if (order != null) {
            order.setStatus(Order.OrderStatus.valueOf(status));
            orderMapper.updateById(order);
        }
        return order;
    }

    @Override
    public Order getOrder(Integer orderId) {
        return orderMapper.selectById(orderId);
    }

    @Override
    public List<Order> getUserOrders(Integer userId) {
        return orderMapper.findByUserId(userId);
    }

    @Override
    public void cancelOrder(Integer orderId) {
        updateOrderStatus(orderId, "CANCELLED");
    }

    @Override
    public void deleteOrder(Integer orderId) {
        orderMapper.deleteById(orderId);
    }
}